package cn.wanghaomiao.crawlers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.scheduling.annotation.Scheduled;

import cn.wanghaomiao.dao.StoreToDbDAO;
import cn.wanghaomiao.model.HouseInfo_58;
import cn.wanghaomiao.seimi.annotation.Crawler;
import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.seimi.struct.Request;
import cn.wanghaomiao.seimi.struct.Response;
import cn.wanghaomiao.utils.SeimiUtils;
import cn.wanghaomiao.xpath.model.JXDocument;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

/**
 * @author 汪浩淼 [et.tw@163.com]
 * @since 2015/10/21.
 */
@Crawler(name = "58house",useCookie = true)
public class HouseCrawler_58 extends BaseSeimiCrawler {

    @Autowired
    private StoreToDbDAO storeToDbDAO;

    private String crawlerRootUrl="http://xa.58.com/ershoufang/0";
    private Integer cronIndex=2;

    @Scheduled(cron = "0/5 * * * * ?")
    public void callByCron() {
        // logger.info("我是一个根据cron表达式执行的调度器，5秒一次");
        // 可定时发送一个Request
        String reqHref = crawlerRootUrl
                .concat("/pn").concat(cronIndex.toString())
                .concat("/?PGTID=0d30000c-001e-31b0-0202-b6a8edb09e0d&ClickID=1");
        push(Request.build(reqHref, "start").setSkipDuplicateFilter(true));
        cronIndex+=1;
    }

    ///ershoufang/0/pn2/? 翻页标签 pn2/?pagesize=10
    //wap:http://wap.58.com/xa/ershoufang/
    //wap:http://m.58.com/xa/ershoufang/25545989147828x.shtml?device=wap&adtype=1&entinfo=25545989147828_0&psid=116632239191433159335724873&ad_id=176723248236824 具体链接
    @Override
    public String[] startUrls() {
        return new String[]{crawlerRootUrl.concat("/?PGTID=0d30000c-001e-31b0-0202-b6a8edb09e0d&ClickID=1")};
    }

    @Override
    public void start(Response response) {
        JXDocument doc = response.document();
        try {
            List<Object> urls = doc.sel("//a[@class='t']/@href");
            logger.info("{}", urls.size());
            for (Object s:urls){
                push(Request.build(s.toString(),"renderBean"));
                //push(Request.build(s.toString(),"test"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void renderBean(Response response){
        try {
            HouseInfo_58 house = response.render(HouseInfo_58.class);
            //logger.info("bean resolve res={},url={}",house,response.getUrl());
            //logger.info(JSON.toJSONString(house));

            if(house.getPosition().split("：").length > 1) 
                house.setPosition(house.getPosition().split("：")[1].trim());
            house.setHref(response.getRealUrl());
            String jsStr=house.getPhoneNumber();
            house.setPhoneNumber(jsStr.substring(jsStr.indexOf("'")+1, jsStr.lastIndexOf("'")));

            //使用神器paoding-jade存储到DB
            int houseId = storeToDbDAO.save(house);
            logger.info("store sus,houseId = {}",houseId);
        } catch (DuplicateKeyException e) {
            logger.error("crawler data duplication...");
        } catch (MySQLIntegrityConstraintViolationException e) {
            logger.error("crawler data violate integrity constraint...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void test(Response response){
        String xPath="//*[@id='t_phone']/child::*/node()";
        try {
            SeimiUtils.xpathTest(response, xPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
